<?php
namespace plugins\store\controller;
use cmf\controller\PluginAdminBaseController;
use think\Db;
use app\common\lib\model_operate\Transit;
use plugins\store\interfaceLib\StoreClassify;
use plugins\store\model\StoreModel;
use plugins\store\model\StoreInfoModel;
use plugins\store\Common as STORECOMMON;
use plugins\store\model\store_rbac\StoreRbacModel;
use tree\Tree;
use plugins\store\validate\StoreValidate;
use plugins\store\model\StoreMemberAdminModel;
use plugins\store\Common;
class AdminIndexController extends PluginAdminBaseController
{
	protected function _initialize()
	{

	}
	public function index()
	{
		$param = $this->request->param();
		$search =  Transit::createTags('search');
		$search->setParam($param);
		$table = Transit::createTags('table');
		$table->setParam($search);
		//权限检测
		$check = ( new Common )->getAllowAccess();
		$where = [];
		if($check === true){
			// $where = ['store_id'=>'admin'];
		}else{
			$where = ['store_id'=>$check];
		}

		$list = $table->query()->where($where)->select();
		$this->assign('search',$search);
		$this->assign('table',$table);
		$this->assign('searchStr',$search->createHtml());
		$this->assign("list",$list);
	    return $this->fetch();
	}
	//店铺信息界面
	public function storeInfo()
	{
		$param = $this->request->param();
		if(!empty($param['noNav'])){
			//店铺id
			$store_id = $param['store_id'];
			$link = (new StoreMemberAdminModel)->get(['store_id'=>$store_id]);
			if($link != null){
				$uid = $link->user_id;
			}else{
				$uid = null;
			}
			// $user = db('user')->field('id')->where(['user_login'=>$login_name])->find();
			// $uid = $user['id'];
			$this->assign('noNav',1);
		}else{
			$uid = cmf_get_current_admin_id();
			$this->assign('noNav',0);
			//链接信息
			$user = db('store_member_admin')->where(['user_id'=>$uid])->find();
			$store_id = $user['store_id'];
		}
		//登陆时间
		$user = db('user')->field('last_login_time')->where(['id'=>$uid])->find();
		$last_login_time = $user['last_login_time'];
		$this->assign('last_login_time',$last_login_time);
		$store = StoreModel::get(['store_id'=>$store_id]);
		if(!empty($store['store_log'])){
            $store['store_log'] = ZY_APP_PATH.$store['store_log'];
        }
		$this->assign('store',$store);
		$store_info = StoreInfoModel::get(['store_id'=>$store_id]);
		$this->assign('store_info',$store_info);
		$path = ZY_APP_PATH;
		$this->assign('path',$path);
		return $this->fetch();
	}
	//编辑店铺
	public function editStore()
	{
		if($this->request->isGet()){
			$get = $this->request->get();
			$store_id = $get['id'];
			$store = StoreModel::get(['store_id'=>$store_id]);
			$this->assign('store',$store);
			$store_info = StoreInfoModel::get(['store_id'=>$store_id]);
			$this->assign('store_info',$store_info);
			return $this->fetch();
		}
		if($this->request->isPost()){
			$post = $this->request->post();
			$result = $this->validate( $post , 'Api.applyStore');
			if(true !== $result){
				return $this->error($result);
			}
			( new StoreModel )->allowField(true)->isUpdate(true)->save($post,['store_id'=>$post['store_id']]);
			( new StoreInfoModel)->allowField(true)->isUpdate(true)->save($post,['store_id'=>$post['store_id']]);
			return $this->success('修改成功');
		}
	}
	//添加子账号
	public function addUser()
	{
		if($this->request->isGet()){
			$get = $this->request->get();
			$this->assign('id',$get['id']);
			return $this->fetch();
		}
		if($this->request->isPost()){
			$post = $this->request->post();
			//当前操作用户id
			$uid = cmf_get_current_admin_id();
			//操作用户旗下店铺
			$store_id = ( new StoreMemberAdminModel)->get(['user_id'=>$uid,'role_id'=>3])->store_id;
			//后台user中添加账号
			//user表添加
			$userid = db('user')->insertGetId(['user_type'=>3,'user_login'=>$post['user_name'],'user_pass'=>cmf_password($post['user_pass'])]);
			if($userid){
				//user_attach表添加
				db('user_attach')->insert(['user_id'=>$userid,'company_id'=>0,'department_id'=>0,'role_id'=>$post['role']]);
				//store_member_admin表添加
				db('store_member_admin')->insert(['user_id'=>$userid,'store_id'=>$store_id,'role_id'=>$post['role']]);
				return $this->success('添加成功');
			}else{
				return $this->error('添加失败');
			}
		}
	}
	//编辑子账号
	public function editUser()
	{
		if($this->request->isGet()){
			$get = $this->request->get();
			$id = $get['id'];
			$info = db('user')->field('id,user_login')->where(['id'=>$id])->find();
			$this->assign('info',$info);
			return $this->fetch();
		}
		if($this->request->isPost()){
			$post = $this->request->post();
			$check = db('user')->where(['id'=>$post['id'],'user_pass'=>cmf_password($post['old_pass'])])->find();
			if($check){
				db('user')->where(['id'=>$post['id']])->update(['user_pass'=>cmf_password($post['new_pass'])]);
				return $this->success('修改成功');
			}else{
				return $this->error('修改失败，请检查密码');
			}
		}
	}
	//删除子账号
	public function delUser()
	{
		$param = $this->request->param();
		//当前操作用户id
		$uid = cmf_get_current_admin_id();
		if($uid == $param['id']){
			return $this->error('无法删除自身账号');
		}
		//store_member_admin表删除
		$result = ( new StoreMemberAdminModel )->where(['user_id'=>$param['id']])->delete();
		if($result){
			//user表删除
			db('user')->where(['id'=>$param['id']])->delete();
			//user_attch表删除
			db('user_attach')->where(['user_id'=>$param['id']])->delete();
			return $this->success('操作成功');
		}else{
			return $this->error('操作失败');
		}
	}
	//恢复店铺
	public function revoer()
	{
		$param = $this->request->post();
		Db::startTrans();
		//所选内容
		$list = ( new StoreModel )
		->where(['store_id'=>['in',$param['id']]])
		->select();
		//遍历
		foreach ($list as $key => $value) {
			if($value->store_status != 4){
				continue;
			}
			$result = $value->save(['store_status'=>2]);
			if(!$result){
				return $this->error($value->store_name.'操作失败');
			}				
		}
		Db::commit();
		return $this->success('操作成功,选中的店铺已启用');
	}
	//禁用店铺
	public function forbidden()
	{
		$param = $this->request->param();
		Db::startTrans();
		//所选内容
		$list = ( new StoreModel )
		->where(['store_id'=>['in',$param['id']]])
		->select();
		//遍历
		foreach ($list as $key => $value) {
			if($value->store_status != 2){
				continue;
			}
			$result = $value->save(['store_status'=>4]);
			if(!$result){
				return $this->error($value->store_name.'操作失败');
			}			
		}
		Db::commit();
		return $this->success('操作成功,选中的店铺已禁用');
	}
	//更改分组
	public function moveUser()
	{
		if($this->request->isGet()){
			$get = $this->request->get();
			$id = $get['id'];
			$role_id = ( new StoreMemberAdminModel )->get(['user_id'=>$id])->role_id;
		    $storeRelevance = STORECOMMON::getStoreRelevanceInfo();//获取当前登录用户的关联信息
		    $where = "id = 3";
		    $where .= " or remark = 'S_".$storeRelevance->store_id."'";
		    $list = db('role')->where($where)->select()->toArray();
			/*tree设置*/
			$tree = new Tree();
			$tree->icon = ['&nbsp;&nbsp;&nbsp;&nbsp│', '&nbsp;&nbsp;&nbsp;&nbsp;├', '&nbsp;&nbsp;&nbsp;&nbsp;└'];
			foreach ($list as $key => &$value) {
				if($value['id'] == $role_id){
					$value['selected'] = "selected='selected'";
				}
			}
			$str = "<option value=\$id \$selected>\$spacer \$name</option>";
			/*tree设置*/
			//内容放入
			$tree->init($list);
			//tree生成
			$select =  $tree->getTree('1',$str);
			$this->assign('role_id',$role_id);
			$this->assign('id',$id);
			$this->assign('select',$select);
			return $this->fetch();
		}
		if($this->request->isPost()){
			$post = $this->request->post();
			$result = ( new StoreMemberAdminModel )->where(['user_id'=>$post['user_id']])->update(['role_id'=>$post['role_id']]);
			if($result){
				db('user_attach')->where(['user_id'=>$post['user_id']])->update(['role_id'=>$post['role_id']]);
				return $this->success('操作成功');
			}else{
				return $this->error('操作失败');
			}
		}
	}
	public function pzUpload(){
		$param = $this->request->param();
		//模块的标识
		$module = $param['_plugin'];
		//文件夹的标识
		$info = $param['info'];
		//获取上传的文件
        $file = $_FILES['file'];	
        $res = fileUpload($file,$module,$info);
        if($res['status'] == true){
        	return json(['save'=>$res['data'],'info'=>$info]);
        }
	}
}
